776. Split BST
Medium

Given the root of a binary search tree (BST) and an integer target, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It Is not necessarily the case that the tree contains a node with the value target.

Additionally, most of the structure of the original tree should remain. Formally, for any child c with parent p in the original tree, if they are both in the same subtree after the split, then node c should still have the parent p.

Return an array of the two roots of the two subtrees.

 

Example 1:

Input: root = [4,2,6,1,3,5,7], target = 2
Output: [[2,1],[4,3,6,null,null,5,7]]

Example 2:

Input: root = [1], target = 1
Output: [[1],[]]

 

Constraints:

  • The number of nodes in the tree is in the range [1, 50].
  • 0 <= Node.val, target <= 1000
Accepted
22,159
Submissions
38,986
Seen this question in a real interview before?
Companies
Related Topics
Similar Questions
Show Hint 1
Use recursion. If root.val <= V, you split root.right into two halves, then join it's left half back on root.right.

Average Rating: 3.45 (71 votes)

Premium

Approach 1: Recursion

Intuition and Algorithm

The root node either belongs to the first half or the second half. Let's say it belongs to the first half.

Then, because the given tree is a binary search tree (BST), the entire subtree at root.left must be in the first half. However, the subtree at root.right may have nodes in either halves, so it needs to be split.


Diagram of tree being split

In the diagram above, the thick lines represent the main child relationships between the nodes, while the thinner colored lines represent the subtrees after the split.

Lets say our secondary answer bns = split(root.right) is the result of such a split. Recall that bns[0] and bns[1] will both be BSTs on either side of the split. The left half of bns must be in the first half, and it must be to the right of root for the first half to remain a BST. The right half of bns is the right half in the final answer.


Diagram of how root tree connects to split of subtree at root.right

The diagram above explains how we merge the two halves of split(root.right) with the main tree, and illustrates the line of code root.right = bns[0] in the implementations.

Complexity Analysis

  • Time Complexity: O(N)O(N), where NN is the number of nodes in the input tree, as each node is checked once.

  • Space Complexity: O(N)O(N).

Report Article Issue

Comments: 29
kenteng's avatar
Read More

Why is this problem medium? It is so convoluted. If given at an interview, I don't think I can write out clear codes like this.

104
Show 1 reply
Reply
Share
Report
calvinchankf's avatar
Read More

Here is a potential follow-up: what if it is an exclusive split? e.g. for a BST root=[1,2,3,4,5], V=3, we want to output [[1,2], [4,5]]

BTW i think this question should also be included in the Explore/Learn session, along with other classic operations such as Search, Insert, Delete and Merge.

700. Search in a Binary Search Tree
701. Insert into a Binary Search Tree
450. Delete Node in a BST
617. Merge Two Binary Trees

FYI

33
Reply
Share
Report
shkleinik's avatar
Read More

What 'bns' stands for?

25
Show 1 reply
Reply
Share
Report
srm012345's avatar
Read More

I got so much grey hair trying to understand what "The left half of bns must be in the first half, and it must be to the right of root for the first half to remain a BST." means.

22
Show 1 reply
Reply
Share
Report
aimjwizardslint's avatar
Read More

time complexity is the height of the tree. at any level, only one node is visited.

14
Reply
Share
Report
StefanPochmann's avatar
Read More

Time Complexity: O(N), where N is the number of nodes in the input tree, as each node is checked once.

You could also say O(height), right? Your "each node is checked once" sounds so absolute, like that's always the case, not just in worst cases.

And hmm... the Python specification says rtype: List[TreeNode] and I always understood that as list. But you don't return a list but a tuple. Does "List" mean "any kind of list", not just list, and does tuple count as "List"? That would at least be non-standard terminology.

18
Reply
Share
Report
eefiasfira's avatar
Read More

Solution presented is inversely proportional to explanation.
Brilliant solution. Really poor explanation. Too bad LeetCode! PLEASE PROOF READ YOUR EXPLANATIONS WITH A NATIVE ENGLISH SPEAKER WHO IS (preferably) NOT A SOFTWARE ENGINEER. Then they'll make sense. Then people who use LeetCode will find more value in their subscriptions. It is easy to write code - hard to explain it in a lucid way. The LeetCode solution writers take the easy way out and leave it to the reader to put 2 and 2 together.

12
Reply
Share
Report
dannyli0818's avatar
Read More

I was thinking about in order traverse, but after seeing this one... forget about it LOL

4
Reply
Share
Report
kofkofkof's avatar
Read More

@kenteng, isn't this the reason why we are looking around here, eh? this question and delete a node in BST are both convoluted to me. guess just need to think more and adapt to those questions.

2
Reply
Share
Report
coder_93's avatar
Read More

What an elegant solution!

3
Reply
Share
Report

You don't have any submissions yet.

/23
Contribute
|||
Saved
Trie
This list is empty.